package com.example.woke1;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_zhcaipeople;
import com.woke.data.Datas_dingqi;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ZhcaidingqiActivity extends Activity implements OnClickListener {

	private Intent intent;
	private View layout_log;
	private Dialog dialog;
	private TextView mTomoney,mTbmoney;
	private EditText mEinmoney;
	private String id;
	private View mProgress;
	private TextView mTlilv,mTdaoxiri,mTdateqixian,mTpeople,mTtotalmoney,mTqixidate,mTqigoumoney,mTlastmoney,mEinday;
	private float bankli = 0;//��������
	private float bankdate = 0;//�̶�����
	private float zhinterest;//�в�����
	
	private String interest;//�õ�����
	private String min_money;//��Ͷ���
	private String exp_money;//ʣ����
	private String date;//Ͷ������
	private String product_title;
	private String product_name;
	private ArrayList<Data_zhcaipeople> datalist = new ArrayList<Data_zhcaipeople>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhcaidingqi);
		
		intent = getIntent();
		id = intent.getStringExtra("id");
		fromwhere = intent.getBooleanExtra("fromwhere", false);
		intview();
		
	}

	private void intview() {
		application = (MyApp) getApplication();
		layout_log = getLayoutInflater().inflate(R.layout.ardialog_cpu, null);
		findViewById(R.id.avzhcaidingqi_image_back).setOnClickListener(this);
		mTok = (TextView) findViewById(R.id.avzhcaidingqi_text_buy);
		mTok.setOnClickListener(this);
		findViewById(R.id.avzhcaidingqi_text_cpu).setOnClickListener(this);
		findViewById(R.id.avzhcaidingqi_relative_people).setOnClickListener(this);
		mPsetprss = (ProgressBar) findViewById(R.id.zhcaidingqi_setprogress);
		mTtitlname = (TextView) findViewById(R.id.avzhcaidingqi_text_titelname);
		mTlilv = (TextView) findViewById(R.id.avzhcaidingqi_text_lilv);//����
		mTdaoxiri = (TextView) findViewById(R.id.avzhcaidingqi_text_daoxidate);//������
		mTdateqixian= (TextView) findViewById(R.id.avzhcaidingqi_text_dateqixian);
		mTlastmoney = (TextView) findViewById(R.id.avzhcaidingqi_text_lastmoney);
		mTpeople = (TextView) findViewById(R.id.avzhcaidingqi_text_people);
		mTqigoumoney= (TextView) findViewById(R.id.avzhcaidingqi_text_qigoumoney);
		mTqixidate = (TextView) findViewById(R.id.avzhcaidingqi_text_qixidate);
		mTtotalmoney = (TextView) findViewById(R.id.avzhcaidingqi_text_totalmoney);
		mygou = findViewById(R.id.avzhcaidingqi_relati_mygou);//�ҵĹ���
		if(fromwhere == false){
			mygou.setVisibility(View.GONE);
		}else{
			mTok.setTextColor(getResources().getColor(R.color.texthint));
			mTok.setClickable(false);
		}
		mTmygou = (TextView) findViewById(R.id.avzhcaidingqi_text_mygou);
		findViewById(R.id.avzhcaidingqi_relative_content).setOnClickListener(this);
		mProgress = findViewById(R.id.zhcaidingqi_progress);
		mTomoney = (TextView) layout_log.findViewById(R.id.ardialog_text_outmoney);
		mTbmoney = (TextView) layout_log.findViewById(R.id.ardialog_text_bankout);
		mEinday = (TextView) layout_log.findViewById(R.id.ardialog_edit_inday);
		mEinmoney = (EditText) layout_log.findViewById(R.id.ardialog_edit_inmoney);
		
		layout_log.findViewById(R.id.ardialog_image_finish).setOnClickListener(this);
//		mEinday.addTextChangedListener(watcher);
		mEinmoney.addTextChangedListener(watcher1);
		getdatainfo();//�˵�����
		getbankinfo();//��������
		getdingqipeople();
	}

	private void getbankinfo() {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(ZhcaidingqiActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "financial");
		params.put("a", "dingqiCount");
		params.put("finanid", id);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						String banklilv = jsonObject.getString("data");
						bankli = Float.parseFloat(banklilv);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mProgress.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mProgress.setVisibility(View.GONE);
			}
		});
	}

	private void getdatainfo() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		if(fromwhere == true){
			params.put("m", "user");
		}else{
			params.put("m", "financial");
		}
		params.put("a", "dingqiDetail");
		params.put("finanid", id);
		if(fromwhere == true){
			params.put("userid", application.getDatas_load().getId());
		}
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("kld", "ld"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						String string = object.getString("data");
						Datas_dingqi data = com.alibaba.fastjson.JSONObject.parseObject(string, Datas_dingqi.class);
//						JSONObject jsonObject = object.getJSONObject("data");
//						String jiexi = jsonObject.getString("jiexi");
						
						product_name = data.getProduct_name();
//						String createdate = jsonObject.getString("createdate");
//						String qixi = jsonObject.getString("qixi");
						date = data.getDate();
//						String type = jsonObject.getString("type");
//						String id = jsonObject.getString("id");
						product_title = data.getProduct_title();
//						product_title = jsonObject.getString("product_title");
//						String product_content = jsonObject.getString("product_content");
						min_money = data.getMin_money();
						interest = data.getInterest();
//						interest = jsonObject.getString("interest");
//						String money = jsonObject.getString("money");
						if(fromwhere == true){
							mTmygou.setText(data.getMybuy().substring(0, data.getMybuy().length()-3)+"元");
						}
						exp_money = data.getExp_money();
						mTtitlname.setText(data.getProduct_name());
						//�껯���� ��ͷ��� ʣ���� Ͷ������
						mTlilv.setText(data.getInterest()+"%");
						mTdaoxiri.setText(data.getJiexi());
						mTdateqixian.setText(data.getDate()+"天");
//						mTpeople.setText(interest+"%");
						mTtotalmoney.setText(data.getMoney()+"元");
						mTqixidate.setText(data.getQixi());
						mTqigoumoney.setText(data.getMin_money()+"元");
						mTlastmoney.setText(data.getExp_money()+"元");
						mEinday.setText("  "+data.getDate());
						bankdate = Float.parseFloat(data.getDate());
						float parseFloat = Float.parseFloat(data.getExp_money());
						zhinterest = Float.parseFloat(data.getInterest());
						float moneys = Float.parseFloat(data.getMoney());
//						int progress = (int) (((moneys - parseFloat )/ moneys)*100);
//						mPsetprss.setProgress(progress);
						
						if(parseFloat == 0f){
							mPsetprss.setProgress(100);
							mTok.setTextColor(getResources().getColor(R.color.texthint));
							mTok.setClickable(false);
						}else if(moneys == parseFloat){
							mPsetprss.setProgress(0);
						}else if(parseFloat > 0){
							int progress = (int) (((moneys - parseFloat) / moneys)*100);
//							holder.mBraepr.setProgress(progress);
							if(progress == 0){
//								if(parseFloat == moneys){
//									mPsetprss.setProgress(0);
//								}else{
									mPsetprss.setProgress(1);
//								}
//								mPsetprss.setProgress(1);
							}else{
//								int progress = (int) ((parseFloat / money)*100);
								mPsetprss.setProgress(progress);
							}
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			
			@Override
			public void onStart() {
				super.onStart();
				mProgress.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mProgress.setVisibility(View.GONE);
			}
		});
	}
	
	private void getdingqipeople() {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(ZhcaidingqiActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "financial");
		params.put("a", "dingqiRecord");
		params.put("finanid", id);
//		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						JSONArray jsonArray = object.getJSONArray("data");
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject jsonObject = jsonArray.getJSONObject(i);
							String createdate = jsonObject.getString("createdate");
							String money = jsonObject.getString("money");
							String userid = jsonObject.getString("userid");
							String id = jsonObject.getString("id");
							String phone = jsonObject.getString("phone");
							String count = jsonObject.getString("count");
							Data_zhcaipeople list = new Data_zhcaipeople(createdate, money, userid, id, phone, count);
							datalist.add(list);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if(datalist.size() > 0 ){
					mTpeople.setText(datalist.size()+"人已购买   ");
				}
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}

	private TextWatcher watcher1 = new TextWatcher() {
	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    }
	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count,
	            int after) {
	    }
	    
	    @Override
	    public void afterTextChanged(Editable s) {
	    	String day1 = ""+mEinday.getText().toString().trim();
	    	String money1 = ""+mEinmoney.getText().toString().trim();
	    	if(!day1.equals("") && !money1.equals("")){
	    		float parseInt1 = Float.parseFloat(day1);
	    		float parseInt0 = Float.parseFloat(money1);
	    		mTomoney.setText(""+parseInt0*zhinterest*0.01+"元");
	    		mTbmoney.setText(""+parseInt0*bankli*0.01+"元");
//	    		mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
	    	}else{
//	    		mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightred));
	    	}
	    }
	};
	private ProgressBar mPsetprss;
	private TextView mTtitlname;
	private TextView mTok;
	private View mygou;
	private TextView mTmygou;
	private MyApp application;
	private boolean fromwhere;
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.avzhcaidingqi_image_back:
			finish();
			break;
			
		case R.id.avzhcaidingqi_text_buy:
			//�껯���� ��ͷ��� ʣ���� Ͷ������
			intent.setClass(ZhcaidingqiActivity.this, DingqibuyActivity.class);
			intent.putExtra("interest", interest);
			intent.putExtra("min_money", min_money);
			intent.putExtra("exp_money", exp_money);
			intent.putExtra("date", date);
			intent.putExtra("product_title", product_name);
			intent.putExtra("id", id);
			startActivity(intent);
			break;
			
		case R.id.avzhcaidingqi_relative_people:
			intent.setClass(ZhcaidingqiActivity.this, DingqipeopleActivity.class);
			intent.putExtra("id", id);
			startActivity(intent);
			break;
			
		case R.id.ardialog_image_finish:
				dialog.dismiss();
			break;
			
		case R.id.avzhcaidingqi_relative_content:
			intent.setClass(ZhcaidingqiActivity.this, ZhcaidinginfoActivity.class);
			intent.putExtra("id", id);
			startActivity(intent);
			break;
			
		case R.id.avzhcaidingqi_text_cpu:
			ViewGroup parent = (ViewGroup) layout_log.getParent();
			if(parent != null){
				parent.removeAllViews();
			}
			
			 dialog = new AlertDialog.Builder(ZhcaidingqiActivity.this).create();
	         dialog.show();
	         dialog.getWindow().setContentView(layout_log);
	         dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
	         dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
			break;

		default:
			break;
		}
	}
}
